#include "const.h"

//
//	!!! THIS IS OLD CODE !!!
//	ignore bad habits and things that don't make sense, kay?
//

// create a series of directories (e.g., lib/build-conf/mswin/config...)
void mkdirs(const char *dirs) {
	struct stat s;
	const char *p;
	char buf[512], *path;

	// ignore the root directory
	if (dirs[0]=='/') {
		buf[0] = dirs[0];
		path = &buf[1];
		p = &dirs[1];
	} else if(dirs[1]==':' && dirs[2]=='/') {
		buf[0] = dirs[0];
		buf[1] = dirs[1];
		buf[2] = dirs[2];
		path = &buf[3];
		p = &dirs[3];
	} else {
		path = &buf[0];
		p = &dirs[0];
	}

	// make each directory, one by one
	while(1) {
		if(*p=='/' || *p==0) {
			*path = 0;

			errno = 0;
			stat(buf, &s);

			if (errno != 0) {
				errno = 0;
				mkdir(buf);
				if (errno) {
					fprintf(stderr, "error: %s: %s [errno:%d]\n",
						buf, strerror(errno), errno);
					exit(1);
				}
			} else if(~s.st_mode & S_IFDIR) {
				fprintf(stderr, "error: %s is not a directory\n", buf);
				fflush(stderr);

				exit(1);
			}

			if (!(*path++ = *p++))
				return;
			else if(*p==0) // handle a '/' ending
				return;
		} else
			*path++ = *p++;

		if (path==&buf[sizeof(buf)-1]) {
			fprintf(stderr, "error: path is too long\n");
			fflush(stderr);

			exit(1);
		}
	}
}
